package com.zofer.study.proxy.jdkdynamicproxy;

import com.zofer.study.proxy.BankCard;
import com.zofer.study.proxy.Card;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * JDK 动态代理类
 * 需要实现InvocationHandler接口
 */
public class JDKProxyCard implements InvocationHandler {
    private Card card;

    public JDKProxyCard(Card card) {
        this.card = card;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDK动态代理去银行办卡路上...");
        method.invoke(card,args);
        System.out.println("JDK动态代理银行卡已办理完成");
        return 1;
    }

    public Card getProxyCard(){
        Card card = (Card) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),new Class[]{Card.class},new JDKProxyCard(new BankCard()));
        return card;
    }
}
